package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.pojo.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/10/10 16:53
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @RequestMapping("/hello")
    public String Hello(){
        return "搜索微服务一辈调用,正常返回结果";
    }

    @RequestMapping("/findById")
    /*
    hystrix线程池代替tomcat完成业务,达到降级和线程隔离的作用
     */
//    hystrix兜底和配置hystrix线程池
    @HystrixCommand(fallbackMethod = "findUserByIdFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public User findUserById(@RequestParam("id") Integer id){
        int i=1/0;
        User user = new User();
        user.setId(1);
        user.setName("沃尔试管熊");
        return user;
    }

//    hystrix兜底方法
    public User findUserByIdFallback(Integer id){
        User user = new User();
        user.setId(1);
        user.setName("发生异常");
        return user;
    }

    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        System.out.println("user = " + user);
        return "添加成功";
    }
}
